package com.woniuxy.counter.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.counter.entity.Entrustment;
import com.woniuxy.counter.entity.MQMessage;
import com.woniuxy.counter.entity.ResponseEntity;
import com.woniuxy.counter.service.EntrustmentSerivce;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("entrustment")
public class EntrustmentController {
    @Autowired
    private EntrustmentSerivce entrustmentSerivce;
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 添加购买委托
     *
     * @param entrustment
     * @return
     */
    @PostMapping("addBuyEntrustment")
    public ResponseEntity<?> addBuyEntrustment(Entrustment entrustment, HttpServletResponse response) {
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        entrustment.setTime(time);
        entrustment.setStatus(Entrustment.WAITBUYING);
        entrustmentSerivce.save(entrustment);
        sendMq(entrustment);
        return ResponseEntity.success;
    }

    /**
     * 添加出售委托
     *
     * @param entrustment
     * @return
     */
    @PostMapping("addSaleEntrustment")
    public ResponseEntity<?> addSaleEntrustment(Entrustment entrustment) {
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        entrustment.setTime(time);
        entrustment.setStatus(Entrustment.WAITSALE);
        entrustmentSerivce.save(entrustment);
        sendMq(entrustment);
        return ResponseEntity.success;
    }

    /**
     * 取消委托
     *
     * @param entrustmentId
     * @return
     */
    @GetMapping("cancelEntrustment")
    public ResponseEntity<?> cancelEntrustment(int entrustmentId) {
        Entrustment entrustment = entrustmentSerivce.getById(entrustmentId);
        entrustment.setStatus(Entrustment.CANCEL);
        sendMq(entrustment);
        return ResponseEntity.success;
    }

    /**
     * 获取所有委托
     *
     * @return
     */
    @GetMapping("getAllEntrustment")
    public ResponseEntity<?> getAllEntrustment() {
        QueryWrapper<Entrustment> wrapper = new QueryWrapper();
        wrapper.eq("status", Entrustment.WAITBUYING).or().eq("status", Entrustment.WAITSALE);
        List<Entrustment> list = entrustmentSerivce.list(wrapper);
        Map<String, List<Entrustment>> map = new HashMap<>();
        for (Entrustment entrustment : list) {
            if (map.containsKey(entrustment.getStockId())) {
                List<Entrustment> entrustments = map.get(entrustment.getStockId());
                entrustments.add(entrustment);
                map.replace(entrustment.getStockId().toString(), entrustments);
            } else {
                List<Entrustment> entrustments = new ArrayList<>();
                entrustments.add(entrustment);
                map.put(entrustment.getStockId().toString(), entrustments);
            }
        }
        sendMq(map);
        return ResponseEntity.success;
    }

    /**
     * 发送mq消息
     *
     * @param entrustment
     */
    private void sendMq(Object entrustment) {
        String jsonEntity = JSON.toJSONString(entrustment);
        String md5JsonEntity = DigestUtils.md5DigestAsHex(jsonEntity.getBytes());
        MQMessage mqMessage = new MQMessage();
        mqMessage.setData(entrustment);
        mqMessage.setMd5Entity(md5JsonEntity);
        mqMessage.setClassName(entrustment.getClass().getName());
        amqpTemplate.convertAndSend("entrustment_exchange", "", JSON.toJSONString(mqMessage));
    }

}
